package com.wyw.base.comm;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.RepositoryService;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wyw.base.util.BaseUtil;
import com.wyw.base.util.FileUploadUtil;
import com.wyw.base.util.WebUtil;
import com.wyw.base.util.fileupload.MultipartUploadParser;
import com.wyw.base.util.fileupload.RequestParser;
import com.wyw.base.util.fileupload.UploadReceiver;
import com.wyw.core.secu.dto.UserDetailInfo;
import com.wyw.core.workflow.service.WorkflowService;

@Controller 
public class CommonManager {
	final Log log = LogFactory.getLog(UploadReceiver.class);
	private static File UPLOAD_DIR = new File("e:/upload/uploads");
	private static File TEMP_DIR = new File("e:/upload/uploadsTemp");
	@Autowired
	private WorkflowService workflowService;
	
	@RequestMapping(value = "/commonManager/fileUpload.biz")
	public @ResponseBody String fileUpload(HttpServletRequest request) throws Exception {
		if (ServletFileUpload.isMultipartContent(request)) {
			MultipartUploadParser multipartUploadParser = new MultipartUploadParser(request, TEMP_DIR, request.getSession().getServletContext());
			RequestParser requestParser = RequestParser.getInstance(request,multipartUploadParser);
			
			BaseUtil.descObject("getCustomParams: ", requestParser.getCustomParams());
			Map customParams =requestParser.getCustomParams();
			//log.info("getFilename: "+requestParser.getFilename());
			//log.info("getOriginalFilename: "+requestParser.getOriginalFilename());
			//log.info("Filename: "+customParams.get("Filename"));
			String fileId=BaseUtil.getUUID();
			String functionCode=(String)customParams.get("functionCode");
			String functionType=(String)customParams.get("functionType");
			
			log.info("functionCode： "+functionCode);
			log.info("functionType： "+functionType);
			
			String fileName=requestParser.getFilename();
			log.info("上传文件名称： "+fileName);
			requestParser.setFilename(fileId);
			
			 
			FileUploadUtil.writeFileForMultipartRequest(requestParser);
		}
		return "{'a':1}";
	}
	
	@RequestMapping(value = "/commonManager/workflow/workflowDeploy.biz")
	public @ResponseBody String workflowDeployUplod(HttpServletRequest request) throws Exception {
		if (ServletFileUpload.isMultipartContent(request)) {
			MultipartUploadParser multipartUploadParser = new MultipartUploadParser(request, TEMP_DIR, request.getSession().getServletContext());
			RequestParser requestParser = RequestParser.getInstance(request,multipartUploadParser);
			
			BaseUtil.descObject("getCustomParams: ", requestParser.getCustomParams());
			Map customParams =requestParser.getCustomParams();
			//log.info("getFilename: "+requestParser.getFilename());
			//log.info("getOriginalFilename: "+requestParser.getOriginalFilename());
			//log.info("Filename: "+customParams.get("Filename"));
			String fileId=BaseUtil.getUUID();
			String functionCode=(String)customParams.get("functionCode");
			String functionType=(String)customParams.get("functionType");
			
			log.info("functionCode： "+functionCode);
			log.info("functionType： "+functionType);
			
			String fileName=requestParser.getFilename();
			String fileNameNet=fileName.substring(0, fileName.lastIndexOf("."));
			log.info("上传文件名称： "+fileName);
			log.info("上传文件名称fileNameNet： "+fileNameNet);
			requestParser.setFilename(fileId);
			
			
			
			FileUploadUtil.writeFileForMultipartRequest(requestParser);
			UserDetailInfo userDetails=WebUtil.getCurrentUserDetail();
			workflowService.deployWorkFlow(userDetails, fileNameNet,FileUploadUtil.UPLOAD_DIR+"/"+fileId);
		}
		return "{'a':1}";
	}
	
	
	
	  
	@RequestMapping(value = "/commonManager/workflow/deploy.biz",method=RequestMethod.POST)
	 public String workflowDeploy(HttpServletRequest request){
		return "com.wyw.biz.comm.manager.workflow.deploy.view";
	}
	
	
}
